package com.edgclub.edg.uc;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.edgclub.edg.R;
import com.edgclub.edg.model.GameUser;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.hyphenate.easeui.model.UserAuth;

import java.util.ArrayList;
import java.util.List;


/**
 * 排名对话框
 * 
 *
 */
public class RankingDialog extends Dialog {

	private TextView num;
	private TextView score;
	private Button btnRestart;
	private ListView listView;
	private Context context;

	private Dialog dialog;

	private SubmitClickListener onSubmitClickListener;

	private RankingDialog instance;

	public static RankingDialog create(Context context, boolean hasCancle) {
		RankingDialog instance = new RankingDialog(context, hasCancle);
		return instance;

	}

	private RankingDialog(Context context, boolean hasCancle) {
		super(context, R.style.MyAlertDialog);
		init(context, hasCancle);
	}

	private void init(Context context, boolean hasCancle) {
		this.context = context;
		WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
		lp.width = context.getResources().getDisplayMetrics().widthPixels;

		View contentView = LayoutInflater.from(context).inflate(
				R.layout.layout_ranking_dialog, null);

		setContentView(contentView, lp);
		num = (TextView) contentView.findViewById(R.id.num);
		score = (TextView) contentView.findViewById(R.id.score);
		btnRestart = (Button) contentView.findViewById(R.id.btn_restart);
		listView = (ListView) contentView.findViewById(R.id.listView);
	}

	/**
	 * 设置确认的监听
	 */
	public void setSubmitListener(
			final SubmitClickListener onSubmitClickListener) {
		btnRestart.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				if (null != onSubmitClickListener) {
					onSubmitClickListener.submitClick();
				}
			}
		});
	}

	public interface SubmitClickListener {
		public void submitClick();

	}
	
	public void setNum(String num) {
		this.num.setText(num);
	}

	public void setScore(String score) {
		this.score.setText(score);
	}
	
	public void setData(ArrayList<GameUser> users){
		RankingAdapter adapter = new RankingAdapter(context);
		adapter.setList(users);
		listView.setAdapter(adapter);
		
	}

	
	public class RankingAdapter extends BaseAdapter {

		public Context context;
		private GlideUtils imageLoaderClient;
		private List<GameUser> list = new ArrayList<GameUser>();

		public RankingAdapter(Context context) {
			this.context = context;
			imageLoaderClient = new GlideUtils();
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}
		
		

		public List<GameUser> getList() {
			return list;
		}

		public void setList(List<GameUser> list) {
			this.list = list;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup group) {
			ViewHolder holder;
			if (null == convertView) {
				convertView = LayoutInflater.from(context).inflate(
						R.layout.item_gz_ranking, null);

				holder = new ViewHolder();
				holder.user_icon = (ImageView) convertView.findViewById(R.id.user_icon);
				holder.tv_num = (TextView) convertView.findViewById(R.id.tv_num);
				holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
				holder.tv_score = (TextView) convertView.findViewById(R.id.tv_score);
				holder.imgv_auth = (ImageView) convertView.findViewById(R.id.imgv_authen);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			
			GameUser user = list.get(position);
			holder.tv_num.setText(position+1+"");
			holder.tv_name.setText(user.getNick()+"");
			holder.tv_score.setText(user.getScore()+ "分");
			imageLoaderClient.Glidde_loadCropCircleImage(context,user.getLogo(),holder.user_icon);
			
			UserAuth userAuth = user.getUserAuth();
			holder.imgv_auth.setVisibility(View.GONE);
			if (userAuth != null) {
				if (userAuth.getType().equals(MineConstant.AUTH_TYPE_EDG)) {
					holder.imgv_auth.setVisibility(View.VISIBLE);
					holder.imgv_auth.setBackgroundResource(R.drawable.v_huan);
				} else if (userAuth.getType().equals(MineConstant.AUTH_TYPE_DAKA)) {
					holder.imgv_auth.setVisibility(View.VISIBLE);
					holder.imgv_auth.setBackgroundResource(R.drawable.v_lan);
				}
			}

			return convertView;
		}

		class ViewHolder {
			private ImageView user_icon;
			private TextView tv_num,tv_name,tv_score;
			private ImageView imgv_auth;
		}

	}

}
